package com.xinqi.modules.discuss.assistant;

import com.xinqi.common.base.response.R;
import com.xinqi.common.core.constant.Constants;
import com.xinqi.common.core.enums.AssistantMessageTypeEnum;
import com.xinqi.common.core.enums.AssistantStateEnum;
import com.xinqi.common.core.enums.AssistantTypeEnum;
import com.xinqi.modules.chat.chat.official.client.ChatAssistantContentClient;
import com.xinqi.modules.chat.chat.official.dto.req.ChatAssistantContentCreateDTO;
import com.xinqi.modules.chat.chat.official.json.BaseMessage;
import com.xinqi.modules.course.clazz.client.ClassClient;
import com.xinqi.modules.course.clazz.client.ClassStudentClient;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.discuss.assistant.service.AssistantDiscussService;
import com.xinqi.modules.discuss.assistant.service.AssistantNoticeService;
import com.xinqi.modules.discuss.notice.domain.CourseNoticeStudentLogEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 新奇助手发送消息公共Service
 */
@Service("assistantListen")
@RequiredArgsConstructor
public class AssistantListen {

    private final ClassClient classClient;

    private final AssistantNoticeService assistantNoticeService;

    private final AssistantDiscussService assistantDiscussService;

    private final ChatAssistantContentClient chatAssistantContentClient;

    /**
     * 新奇助手公共发布方法
     * 课程内容ID contentId
     * 作业轮数 times
     */
    public void releaseCourseContent(Long contentId, AssistantMessageTypeEnum type) {
        switch (type) {
            case NOTICE:
                chatAssistantContentClient.batchInsert(assistantNoticeService.sentAssistantNotice(contentId));
                break;
            case DISCUSS:
                chatAssistantContentClient.batchInsert(assistantDiscussService.releaseAssistantDiscuss(contentId));
                break;
            default:
                break;
        }
    }

    /**
     * 公告一键提醒
     */
    public void sentNoticeRemind(Long contentId, List<CourseNoticeStudentLogEntity> students) {
        chatAssistantContentClient.batchInsert(assistantNoticeService.sentAssistantNoConfirmNotice(contentId, students));
    }


    /**
     * 初始化发布基本消息体
     */
    public ChatAssistantContentCreateDTO initChatAssistantContent(AssistantMessageTypeEnum type) {
        // 初始化基本信息
        ChatAssistantContentCreateDTO assistant = new ChatAssistantContentCreateDTO();
        assistant.setType(AssistantTypeEnum.RELEASE.getCode());
        assistant.setMessageType(type.getType());
        assistant.setProgressState(AssistantStateEnum.PROGRESS.getCode());
        return assistant;
    }

    /**
     * 初始化发布基本消息内容
     */
    public BaseMessage initBaseMessage(Long classId) {
        // 获取班级信息
        ClassResultDTO classResult = R.as(classClient.findClass(classId));
        BaseMessage message = new BaseMessage();
        if (Objects.nonNull(classResult)) {
            message.setCourseName(classResult.getCourseName() + Constants.SPACE_DROP + classResult.getName());
            message.setClassId(classId);
        } else {
            message.setCourseName("" + Constants.SPACE_DROP + "");
            message.setClassId(classId);
        }
        return message;
    }

}
